#!/usr/local/bin/python3
# -*- coding: utf-8 -*-

"""
@File    : jmp_box.py
@Author  : Link
@Time    : 2022/3/27 17:37
@Mark    :
"""
from string import Template
from typing import List


class JmpyBox:
    """
    JMP Window,Box
    """

    @staticmethod
    def jPlatForm(plot: List[str], dt: str = "dt", pt: str = "pt"):
        """
        用于返回生成的显示框, 避免提前显示了UI
        Close All( Data Tables, NoSave );
        Close All( Reports, NoSave );
        Close All( Journals, NoSave );
        dt_0 = Open( "Wk811Data\WK_TY_0_J.csv", Invisible, Import Settings( Labels( 1 ), Column Names Start( 1 ), Data Starts( 2 ), ) );
        dt_1 = Open( "Wk811Data\WK_TY_1_J.csv", Invisible, Import Settings( Labels( 1 ), Column Names Start( 1 ), Data Starts( 2 ), ) );
        dt_2 = Open( "Wk811Data\WK_TY_2_J.csv", Invisible, Import Settings( Labels( 1 ), Column Names Start( 1 ), Data Starts( 2 ), ) );
        plot_0 = Platform(
            dt_0,
            Outline Box( "Wk811Data\WK_TY_0.csv",
                Hierarchical Cluster(
                    Y( :FAIL_FLAG ),
                    Object ID( :lotName, :waferIndex ),
                    Attribute ID( :X_COORD, :Y_COORD ),
                    Method( "Ward" ),
                    Standardize Data( 0 ),
                    Cluster Summary( 1 ),
                    Dendrogram Scale( "Distance Scale" ),
                    Number of Clusters( 20 ),
                    Add Spatial Measures( Attributes( 1 ), Angle( 1 ), Radius( 1 ), Streak Angle( 1 ), Streak Distance( 1 ) ),
                    SendToReport(
                        Dispatch( {}, "Dendrogram", OutlineBox, {Close( 1 ), SetHorizontal( 1 )} ),
                        Dispatch( {"Dendrogram"}, "Clust Dendro", FrameBox, {Frame Size( 35, 700 )} )
                    )
                )
            )
        );
        plot_1 = Platform(
            dt_1,
            Outline Box( "Wk811Data\WK_TY_1.csv",
                Hierarchical Cluster(
                    Y( :FAIL_FLAG ),
                    Object ID( :lotName, :waferIndex ),
                    Attribute ID( :X_COORD, :Y_COORD ),
                    Method( "Ward" ),
                    Standardize Data( 0 ),
                    Cluster Summary( 1 ),
                    Dendrogram Scale( "Distance Scale" ),
                    Number of Clusters( 20 ),
                    Add Spatial Measures( Attributes( 1 ), Angle( 1 ), Radius( 1 ), Streak Angle( 1 ), Streak Distance( 1 ) ),
                    SendToReport(
                        Dispatch( {}, "Dendrogram", OutlineBox, {Close( 1 ), SetHorizontal( 1 )} ),
                        Dispatch( {"Dendrogram"}, "Clust Dendro", FrameBox, {Frame Size( 35, 700 )} )
                    )
                )
            )
        );
        plot_2 = Platform(
            dt_2,
            Outline Box( "Wk811Data\WK_TY_2.csv",
                Hierarchical Cluster(
                    Y( :FAIL_FLAG ),
                    Object ID( :lotName, :waferIndex ),
                    Attribute ID( :X_COORD, :Y_COORD ),
                    Method( "Ward" ),
                    Standardize Data( 0 ),
                    Cluster Summary( 1 ),
                    Dendrogram Scale( "Distance Scale" ),
                    Number of Clusters( 20 ),
                    Add Spatial Measures( Attributes( 1 ), Angle( 1 ), Radius( 1 ), Streak Angle( 1 ), Streak Distance( 1 ) ),
                    SendToReport(
                        Dispatch( {}, "Dendrogram", OutlineBox, {Close( 1 ), SetHorizontal( 1 )} ),
                        Dispatch( {"Dendrogram"}, "Clust Dendro", FrameBox, {Frame Size( 35, 700 )} )
                    )
                )
            )
        );
        win1 = New Window( "聚类分析报告", Outline Box( "Table", H List Box( plot_0, plot_1 ), H List Box( plot_2 ) ) );
        win1 << Save Interactive HTML( "D:\\testJmpRdCsvDoMultiCluGenHtml.html" );
        Run Program( Executable( "CMD.EXE"/*path probably not needed*/ ), Options( {"/c", "D:\\testJmpRdCsvDoMultiCluGenHtml.html"} ), );
        win1 << Close Window;
        Close All( Data Tables, NoSave );
        Close All( Reports, NoSave );
        Close All( Journals, NoSave );
        """

        plot_str = ";".join(plot)
        return Template(
            """
$pt = Platform( $dt, $plot_str );
            """
        ).substitute(pt=pt, dt=dt, plot_str=plot_str)

    @staticmethod
    def jPngBox(filepath: str) -> str:
        """
        读取图片
        :param filepath:
        :return:
        """
        return Template(
            """
Picture Box(Open( "$filepath", png))
            """
        ).substitute(filepath=filepath)

    @staticmethod
    def jNewWin(*boxes: str, title: str = "Window", rbiv="rbiv") -> str:
        return Template("""
$rbiv = New Window( "$title",
$boxes
);
        """).substitute(rbiv=rbiv, title=title, boxes=",\n".join(boxes))

    @staticmethod
    def jOutlineBox(*boxes: str, title: str = "Table") -> str:
        return Template("""
Outline Box( "$title",
$boxes
);
        """).substitute(title=title, boxes=",\n".join(boxes))

    @staticmethod
    def jVListBox(*graph_builders) -> str:
        return Template("""
V List Box(
    $graphs
);
        """).substitute(graphs=",\n".join(graph_builders))

    @staticmethod
    def jHListBox(*graph_builders) -> str:
        return Template("""
H List Box(
    $graphs
);
        """).substitute(graphs=",\n".join(graph_builders))

    @staticmethod
    def jGroupBox(*graph_builders: str, col: int = 2, title: str = "Table") -> str:
        """
        return: new_outline_box
        """
        temp_h = []
        boxes = []
        for index, arg in enumerate(graph_builders):
            temp_h.append(arg)
            if (index + 1) % col == 0:
                boxes.append(JmpyBox.jHListBox(*temp_h))
                temp_h.clear()
        if temp_h:
            boxes.append(JmpyBox.jHListBox(*temp_h))
        return JmpyBox.jOutlineBox(*boxes, title=title)

    @staticmethod
    def jGroupItem(*graph_builders: str, col: int = 2) -> list:
        """
        return: new_outline_box
        """
        temp_h = []
        boxes = []
        for index, arg in enumerate(graph_builders):
            temp_h.append(arg)
            if (index + 1) % col == 0:
                boxes.append(JmpyBox.jHListBox(*temp_h))
                temp_h.clear()
        if temp_h:
            boxes.append(JmpyBox.jHListBox(*temp_h))
        return boxes
